package com.smartstate.IOC.controller;

import com.google.common.collect.Lists;
import com.smartstate.IOC.entity.AssetSituation;
import com.smartstate.IOC.service.AssetSituationService;
import com.smartstate.common.web.httpResult.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags = {"资产"})
@RequestMapping("/Asset")
public class AssetSituationController {

    @Autowired
    private AssetSituationService assetSituationService;

    @RequestMapping(value = "/getInherentAssets", method = RequestMethod.GET)
    @ApiOperation(value = "资产态势-园区楼栋固有资产")
    public ResultVO<List<AssetSituation>>  getInherentAssets(){
        return ResultVO.success(assetSituationService.getAssetsByType("固定资产"));
    }

    @RequestMapping(value = "/getAuxiliaryEquipment", method = RequestMethod.GET)
    @ApiOperation(value = "资产态势-园区楼栋辅助设备")
    public ResultVO<List<AssetSituation>>  getAuxiliaryEquipment(){
        return ResultVO.success(assetSituationService.getAssetsByType("辅助设备"));
    }

    @RequestMapping(value = "/getProductionEquipment", method = RequestMethod.GET)
    @ApiOperation(value = "资产态势-园区楼栋生产设备")
    public ResultVO<List<AssetSituation>>  getProductionEquipment(){
        return ResultVO.success(assetSituationService.getAssetsByType("生产设备"));
    }

    @RequestMapping(value = "/getProductionTools", method = RequestMethod.GET)
    @ApiOperation(value = "资产态势-园区楼栋工具")
    public ResultVO<List<AssetSituation>>  getProductionTools(){
        return ResultVO.success(assetSituationService.getAssetsByType("工具"));
    }

    @RequestMapping(value = "/getProductionGoods", method = RequestMethod.GET)
    @ApiOperation(value = "资产态势-园区楼栋产品")
    public ResultVO<List<Map<String,Object>>> getProductionGoods(){
//    public ResultVO<List<AssetSituation>> getProductionGoods(){
//        return ResultVO.success(assetSituationService.getAssetsByType("产品"));
        List<Map<String,Object>> result = Lists.newArrayList();

        Map<String,Object> map1 = new HashMap<>();
        map1.put("storageName", "仓1");
        map1.put("materialType", "原料");
        map1.put("materialNum", 1000);
        map1.put("storageUsage", "32");
        result.add(map1);

        Map<String,Object> map2 = new HashMap<>();
        map2.put("storageName", "仓2");
        map2.put("materialType", "前期半成品");
        map2.put("materialNum", 70000);
        map2.put("storageUsage", "32");
        result.add(map2);

        Map<String,Object> map3 = new HashMap<>();
        map3.put("storageName", "仓3");
        map3.put("materialType", "中期半成品");
        map3.put("materialNum", 2300);
        map3.put("storageUsage", "32");
        result.add(map3);

        Map<String,Object> map4 = new HashMap<>();
        map4.put("storageName", "仓4");
        map4.put("materialType", "成品");
        map4.put("materialNum", 600);
        map4.put("storageUsage", "32");
        result.add(map4);
        return ResultVO.success(result);
    }

    @RequestMapping(value = "/getCategoryCounts", method = RequestMethod.GET)
    @ApiOperation(value = "资产态势-资产种类数量占比")
    public ResultVO<Map<String, Long>> getCategoryCounts(){
       List<AssetSituation> assets = assetSituationService.getAssetsByType(null);
       Map<String, Long> categoryCounts = assets.stream().collect(Collectors.groupingBy(AssetSituation :: getAssetType, Collectors.counting()));
        categoryCounts.put("total", new Long(assets.size()));
        return ResultVO.success(categoryCounts);
    }

}